package yns.springboot.guide.demo.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CommonExceptionHandler {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	//返回到指定页面
//	@ExceptionHandler(CommonExceptionInfo.class) // 可以直接写@ExceptionHandler,不指明异常类，会自动映射
//	public String commonExceptionHnadler(CommonException commonException) { // 还可以声明接收其他任意参数
//		logger.info(commonException.toString());
//		return "../page/error.html";
//	}
	
	
	//返回指定字符串(可以作为接口返回出现异常时的数据)
	@ResponseBody
	@ExceptionHandler(CommonExceptionInfo.class) // 可以直接写@ExceptionHandler,不指明异常类，会自动映射
	public String commonExceptionHnadler2(CommonExceptionInfo commonException) { // 还可以声明接收其他任意参数
		logger.info(commonException.toString());
		return commonException.toString();
	}

	@ExceptionHandler(Exception.class) // 可以直接写@EceptionHandler，IOExeption继承于Exception
	public String baseExceptionHandler(Exception exception) {
		logger.info(exception.toString());
		return "/page/error.html";
	}

}
